package com.sicilin.common.ajax.vo;

import com.sicilin.common.enums.ResultStateEnum;
import net.sf.json.JSONObject;

/**
 * 用于通用性ajax请求的返回
 * Created by sicilin on 2017/8/1.
 */
public class AjaxResponse {
    private int error;
    private String errorMsg;
    private Object data;

    public AjaxResponse(){}

    public AjaxResponse(Exception e){
        this.error = 1;
        this.errorMsg = e.getMessage();
        this.data = null;
    }
    public AjaxResponse(ResultStateEnum resultStateEnum){
        if(resultStateEnum == ResultStateEnum.SUCCESS){
            this.error = 0;
            this.errorMsg = null;
            this.data = "success";
        }else{
            this.error = 1;
            this.errorMsg = "error";
            this.data = null;
        }
    }

    public AjaxResponse(Object data){
        this.error = 0;
        this.errorMsg = null;
        this.data = data;
    }

    public int getError() {
        return error;
    }

    public void setError(int error) {
        this.error = error;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public String generateJSONString(){
        JSONObject result = JSONObject.fromObject(this);
        return result.toString();
    }
}
